//
// Created by 11510 on 2025/10/2.
//

#include "SaveCommand.h"

xclib_client::SaveCommand::SaveCommand(CommandContext ctx) : ICommand("save", ctx) {};

std::optional<std::future<int>> xclib_client::SaveCommand::run(const std::vector<std::string> &args, bool wait) {
    cxxopts::Options o("save", "save");
    o.add_options()
            ("u,unitmap", "unitmap", cxxopts::value<std::string>()->default_value(""))
            ("p,path", "path",cxxopts::value<std::string>()->default_value(""))
            ("help","help");
    try {
        auto r = parseOptions(o, args);
        if (r.count("help")) {
            std::cout << o.help() << "\n";
            return std::nullopt;
        }
        std::string u = r["unitmap"].as<std::string>();
        std::string p = r["path"].as<std::string>();
        auto task = [u, p]() {
            std::cout << "Saving unit '" << u << "' to path '" << p << "'...\n";
            return 0;
        };
        if (!wait) return make_optional(std::async(std::launch::async, task));
        else {
            task();
            return std::nullopt;
        }
    } catch (const cxxopts::exceptions::exception &e) {
        std::cerr << "save: " << e.what() << "\n";
        return std::nullopt;
    }
}

std::string xclib_client::SaveCommand::shortHelp() const { return "save -u <unitmap> -p <path>"; }

std::string xclib_client::SaveCommand::longHelp() const { return "save -u <unitmap> -p <path>\n    save to disk"; }